package com.government.web.controller.system;

import cn.hutool.core.util.StrUtil;
import com.government.common.enums.SmTemplateCode;
import com.government.common.utils.CacheUtils;
import com.government.framework.web.service.VerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.government.common.core.controller.BaseController;
import com.government.common.core.domain.AjaxResult;
import com.government.common.core.domain.model.RegisterBody;
import com.government.common.utils.StringUtils;
import com.government.framework.web.service.SysRegisterService;
import com.government.system.service.ISysConfigService;

/**
 * 注册验证
 * 
 * @author system
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private VerifyCodeService verifyCodeService;


    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String key = verifyCodeService.verify(user.getPhone(), SmTemplateCode.REGISTER,user.getCode());
        String msg = registerService.register(user);
        if(StrUtil.isNotBlank(key)){
            CacheUtils.getRedisTemplate().delete(key);
        }
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    @PostMapping("/forgetPwd")
    public AjaxResult forgetPwd(@RequestBody RegisterBody user)
    {

        String key = verifyCodeService.verify(user.getPhone(), SmTemplateCode.FORGET_PASS,user.getCode());
        registerService.forgetPwd(user);
        if(StrUtil.isNotBlank(key)){
            CacheUtils.getRedisTemplate().delete(key);
        }
        return AjaxResult.success();
    }
}
